# -*- coding:utf8 -*- #
# -----------------------------------------------------------------------------------
# ProjectName:   mqtt_connection
# FileName:     get_path
# Author:
# Datetime:    2022/4/11 15:17
# Description：
# -----------------------------------------------------------------------------------
import configparser
import os
import sys


class ConfigUtil:
    def __init__(self):
        # 实例化对象
        self.config = configparser.ConfigParser()
        # 获取项目文件路径后通过common进行切分,选择第一段self.beforePath = ""
        if getattr(sys, 'frozen', False):
            self.beforePath = os.path.dirname(os.path.realpath(sys.executable)).split("dist")[0]
        elif __file__:
            self.beforePath = os.path.dirname(os.path.abspath(__file__)).split("common")[0]
        # 找到inin文件路径
        self.config.read(fr"{self.beforePath}conf/configIni/config.ini", encoding="utf-8-sig")

    def get_connackData_path(self) -> str:
        # 获取ini文件配置的文件
        file_path = self.config.get("FilePath", "connackDataPath")
        # 进行文件路径拼接
        return os.path.join(self.beforePath, file_path)

    def get_sql_json_path(self) -> str:
        file_path = self.config.get("FilePath", "sqlJsonPath")
        return os.path.join(self.beforePath, file_path)

    def get_database_status(self) -> bool:
        """
            ;True：线上环境
            ;False：测试环境
        """
        status = self.config.get("Database_status", "dataBaseEnvironment")
        if status.lower() == "true":
            return True
        else:
            return False

    def get_log_path(self, logType: int) -> str:
        """
        logType=1,mqttlog,logType=2,chargelog;
        True：线上环境,False：测试环境
        """
        dataBaseStatus = self.get_database_status()
        if logType == 1 and dataBaseStatus is True:
            file_path = self.config.get("FilePath", "mqttLogPro")
            return os.path.join(self.beforePath, file_path)
        elif logType == 1 and dataBaseStatus is False:
            file_path = self.config.get("FilePath", "mqttLogTest")
            return os.path.join(self.beforePath, file_path)
        elif logType == 2 and dataBaseStatus is True:
            file_path = self.config.get("FilePath", "chargeLogPro")
            return os.path.join(self.beforePath, file_path)
        elif logType == 2 and dataBaseStatus is False:
            file_path = self.config.get("FilePath", "chargeLogTest")
            return os.path.join(self.beforePath, file_path)
        else:
            print("传参错误，暂未找到相关log路径！")

    # 声明方法，读取配置文件中的数据库信息
    def get_database_info(self, option: str) -> str:
        if self.get_database_status() is True:
            return self.config.get("Database_Por", option)
        else:
            return self.config.get("Database_Test", option)

    def get_mqtt_url(self, deviceName: str):
        if self.get_database_status() is True:
            if deviceName.startswith("C"):
                return self.config.get("MQTT", "mqtt_pro_collector_url")
            else:
                return self.config.get("MQTT", "mqtt_pro_meter_url")
        else:
            if deviceName.startswith("C"):
                return self.config.get("MQTT", "mqtt_test_collector_url")
            else:
                return self.config.get("MQTT", "mqtt_test_meter_url")

    def get_mqtt_config(self, option: str):
        return self.config.get("MQTT", option)

    def get_productkey(self):
        if self.get_database_status() is True:
            return self.config.get("ProductKey", "Pro_productKey")
        else:
            return self.config.get("ProductKey", "Test_productKey")


if __name__ == '__main__':
    print(ConfigUtil().get_mqtt_config("deviceName"))
    print(ConfigUtil().get_log_path(2))
